package main

import (
	"github.com/zhuCheer/libra"
	"net/http"
)

func main() {
	go httpsrv01()
	go httpsrv02()
	go httpsrv03()

	// In normal conditions, proxyAddr is a ip address, domain is a domain name
	// It is just an example, so the proxyAddr is the same with domain
	proxyAddr := "127.0.0.1:5000"
	domain := "127.0.0.1:5000"

	var srv = libra.NewHttpProxySrv(proxyAddr, nil)
	srv.RegistSite(domain, "roundrobin", "http")
	srv.AddAddr(domain, "127.0.0.1:5001", 1)
	srv.AddAddr(domain, "127.0.0.1:5002", 1)
	srv.AddAddr(domain, "127.0.0.1:5003", 1)
	srv.Start()
}

func httpsrv01() {

	mux := http.NewServeMux()
	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "text/html; charset=UTF-8")
		w.Write([]byte("view http server 01 <br> <img src='" + proxy01 + "'><br> source code: <a href='https://github.com/zhuCheer/libra'>https://github.com/zhuCheer/libra</a>"))
	})
	http.ListenAndServe(":5001", mux)
}

func httpsrv02() {

	mux := http.NewServeMux()
	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "text/html; charset=UTF-8")
		w.Write([]byte("view http server 02 <br> <img src='" + proxy02 + "'>"))
	})
	http.ListenAndServe(":5002", mux)
}

func httpsrv03() {

	mux := http.NewServeMux()
	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "text/html; charset=UTF-8")
		w.Write([]byte("view http server 03<br> <img src='" + proxy03 + "'>"))
	})
	http.ListenAndServe(":5003", mux)
}

var proxy01 = ``

var proxy02 = ``

var proxy03 = ``
